//
//  ShopToolsView.m
//  sensen_ios
//
//  Created by pro on 16/2/25.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopToolsView.h"

#import "ShopVC.h" // 商城首页
#import "ShopConsultVC.h" // 购买咨询

@interface ShopToolsView ()


@end

@implementation ShopToolsView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1];
        
        self.width = SCREEN_WIDTH;
        self.height = 49;
        
        [self _initViews];

    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    [self _createLeftViews];
    [self _createRightViews];
    
}
// 创建左边的三个视图（客服，商城，收藏）
- (void)_createLeftViews {
    
    NSArray *titles = @[@"客服", @"商城", @"收藏"];
    NSArray *imgNames = @[@"商品详情_客服", @"商品详情_商城", @"商品详情_未收藏"];
    CGFloat width = self.width / 2 / 3;
    CGFloat height = self.height;
    CGFloat x = 0, y = 0;
    for (int i = 0; i < titles.count; i ++) {
        width = self.width / 2 / 3;
        height = self.height;
        x = i * width;
        y = 0;
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self addSubview:bgView];
        bgView.tag = i + 1; // tag
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [bgView addGestureRecognizer:tap];
        
        // 图片
        width = bgView.width * 0.4;
        height = width;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imgView.image = [UIImage imageNamed:imgNames[i]];
        [bgView addSubview:imgView];
        imgView.tag = 10; // tag
  
        // 文字
        width = bgView.width;
        height = 10;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        titleLabel.userInteractionEnabled = YES;
        titleLabel.font = FONT(10);
        titleLabel.text = titles[i];
        titleLabel.textColor = [UIColor darkGrayColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [bgView addSubview:titleLabel];
        titleLabel.tag = 11; // tag
        
        // 排位置
        imgView.top = (bgView.height - imgView.height - titleLabel.height - 2) / 2;
        imgView.center = CGPointMake(bgView.width / 2, imgView.center.y);
        titleLabel.top = imgView.bottom + 4;
        titleLabel.center = CGPointMake(bgView.width / 2, titleLabel.center.y);
        
        // 分割线
        width = 1;
        height = self.height;
        x = (i + 1) * bgView.width - 0.5;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [self addSubview:line];
    }
}
// 创建右边的两个视图（加入购物车，立即购买）
- (void)_createRightViews {
    NSArray *titles = @[@"加入购物车", @"立即购买"];
    NSArray *colors = @[ORANGE_COLOR, RED_COLOR];
    CGFloat x, y , width, height;
    for (int i = 0; i < titles.count; i ++) {
        width = self.width / 2 / 2;
        height = self.height;
        x = self.width / 2 + i * width;
        y = 0;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        titleLabel.userInteractionEnabled = YES;
        titleLabel.font = FONT(15);
        titleLabel.text = titles[i];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = colors[i];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        titleLabel.tag = 4 + i;
        
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [titleLabel addGestureRecognizer:tap];
    }
}



#pragma mark - 点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    switch (tap.view.tag) {
        case 1:
            [self _customerServiceAction]; // 联系客服
            break;
        case 2:
            [self _shopAction]; // 商城
            break;
        case 3:
            [self _collectAction]; // 收藏
            break;
        case 4:
            [self _addToCart]; // 加入购物车
            break;
        case 5:
            [self _buyNowAction]; // 立即购买
            break;
        default:
            break;
    }
}
// 客服
- (void)_customerServiceAction {
    
    ShopConsultVC *vc = [[ShopConsultVC alloc] init];
    vc.pid = _pid;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

// 商城
- (void)_shopAction {
    // 从栈中找到商城首页并返回
    for (UIViewController *vc in self.viewController.navigationController.viewControllers) {
        if ([vc isKindOfClass:[ShopVC class]]) {
            [self.viewController.navigationController popToViewController:vc animated:YES];
        }
    }
}
// 收藏
- (void)_collectAction {
    
    // 点击加入购物车的操作 由代理实现
    if ([self.delegate respondsToSelector:@selector(shopToolsViewDidClick:)]) {
        [self.delegate shopToolsViewDidClick:3];
    }
}
// 加入购物车
- (void)_addToCart {
    
    // 点击加入购物车的操作 由代理实现
    if ([self.delegate respondsToSelector:@selector(shopToolsViewDidClick:)]) {
        [self.delegate shopToolsViewDidClick:4];
    }
}
// 立即购买
- (void)_buyNowAction {
    
    // 点击加入购物车的操作 由代理实现
    if ([self.delegate respondsToSelector:@selector(shopToolsViewDidClick:)]) {
        [self.delegate shopToolsViewDidClick:5];
    }
}


#pragma mark - 重写set
- (void)setIsCollected:(BOOL)isCollected {
    _isCollected = isCollected;
    
    UIView *collectView = [self viewWithTag:3];
    UIImageView *imgView = (UIImageView *)[collectView viewWithTag:10];
    imgView.image = [UIImage imageNamed:(_isCollected?@"商品详情_已收藏":@"商品详情_未收藏")];
    UILabel *titleLabel = (UILabel *)[collectView viewWithTag:11];
    titleLabel.text = _isCollected?@"已收藏":@"收藏";
}

@end
